//
//  MTOpenRedpackDetailHeaderView.swift
//  MT
//
//  Created by ooops on 2019/12/24.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
import MTCategory

class MTOpenRedpackDetailHeaderView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        makeConstraint()
        //clipsToBounds = true
        backgroundColor = UIColor(hex: 0xf4f4f4)
    }
    
    var openRedpackModel: MTOpenRedpackModel? {
        didSet {
            if openRedpackModel != nil {
                if let avator = openRedpackModel!.result?.sendUserHeadHref {
                    avatorImgView.kf.setImage(with: URL(string: avator))
                }
                
                if let remark = openRedpackModel!.result?.remark {
                    remarkLabel.text = remark
                }
                
//                if let money = openRedpackModel?.result?.money {
//                    redpackMoneyCount.text = "\(money)"
//                }
                
                if let status = openRedpackModel?.result?.status {
                    /// 如果是打开了自己的红包
                    if status == .owner, let money = openRedpackModel!.result?.money {
                        //descripLabel.text = "红包金额\(money)钻石, 等待对方领取"
                        //redpackMoneyCount.attributedText = attributePullMoney("\(money)")
                        descripLabel.text = "等待对方领取红包"
                    } else if status == .invalid, let st = openRedpackModel?.sendType, st == 1 { /// 失效状态的个人红包
                        descripLabel.text = "红包已超过24小时，钻石已退至您余额"
                    } else {
                        if let outNum = openRedpackModel!.result?.outNum, let num = openRedpackModel!.result?.num, let sendType = openRedpackModel?.sendType, sendType == 2, let money = openRedpackModel!.result?.money {
                            descripLabel.text = "已领取\(outNum)/\(num), 红包总金额\(money)点券"
                        } else {
                            descripLabel.text = "红包已被领取"
                        }
                        
                        if let money = openRedpackModel!.result?.money {
                            redpackMoneyCount.attributedText = attributePullMoney("\(money)")
                        }
                    }
                }
            }
        }
    }
    
    
    private func attributePullMoney(_ moneyString: String) -> NSAttributedString {
        
        let attributeText: [NSAttributedString.Key : Any] = [.font : PingFangSCRegularFont(14), .foregroundColor : UIColor.text.x1d1a1a]
        
        let attributeString = NSMutableAttributedString(string: "\(moneyString)点券", attributes: attributeText)
        
        let attributeNum: [NSAttributedString.Key : Any] = [.font : PingFangSCSemiboldFont(24), .foregroundColor : UIColor.text.x1d1a1a]
        
        let attributeNumRange: NSRange = NSRange(location: 0, length: moneyString.count)
        
        attributeString.addAttributes(attributeNum, range: attributeNumRange)
        
        return attributeString
    }
    
    private func setupUI() {
        addSubview(circleImgView)
        addSubview(remarkLabel)
        addSubview(avatorImgView)
        addSubview(redpackMoneyCount)
        addSubview(descripLabel)
        addSubview(bgColorView)
    }
    
    private func makeConstraint() {
        circleImgView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalTo(self.snp.top).offset(-20)
            make.left.equalToSuperview().offset(-40)
            make.right.equalToSuperview().offset(40)
            make.height.equalTo(circleImgView)
        }
        
        avatorImgView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            //make.bottom.equalTo(circleImgView.snp.bottom).offset(-41.auto())
            make.centerY.equalTo(circleImgView.snp.bottom)
            make.width.height.equalTo(96.auto())
        }
        
        remarkLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(avatorImgView.snp.bottom).offset(8)
        }
        
        
        redpackMoneyCount.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(remarkLabel.snp.bottom).offset(5.auto())
        }
        
        
        descripLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10.auto())
            make.bottom.equalToSuperview().offset(-10.auto())
        }
        
        bgColorView.snp.makeConstraints { (make) in
            make.bottom.equalTo(self.snp.top)
            make.left.right.equalToSuperview()
            make.height.equalTo(ScreenHeight)
        }
    }
    
    lazy var bgColorView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(hex: 0xF35C4B)
        return v
    }()
    
    lazy var remarkLabel: UILabel = {
        let l = UILabel()
        l.textColor = UIColor.text.x1d1a1a
        l.font = PingFangSCMediumFont(10)
        l.text = ""
        l.textAlignment = .center
        return l
    }()
    
    lazy var avatorImgView: UIImageView = {
        let imgV = UIImageView()
        imgV.layer.cornerRadius = 48.auto()
        imgV.layer.masksToBounds = true
        imgV.backgroundColor = .white
        imgV.contentMode = .scaleAspectFill
        imgV.layer.borderColor = UIColor.white.cgColor
        imgV.layer.borderWidth = 1
        return imgV
    }()
    
    lazy var redpackMoneyCount: UILabel = {
        let l = UILabel()
        l.textColor = UIColor.text.x1d1a1a
        l.text = ""
        return l
    }()
    
    lazy var descripLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCRegularFont(14)
        l.textColor = UIColor.text.x9b9b9b
        l.text = ""
        return l
    }()
    
    lazy var circleImgView: UIImageView = {
        let imgV = UIImageView()
        let image = UIImage(color: UIColor(hex: 0xF35C4B), size: CGSize(width: ScreenWidth, height: ScreenWidth))?.roundCorners(cornerRadius: ScreenWidth * 0.5)?.innerShadowedImage(shadowColor: UIColor(hex: 0xF7286F, alpha: 0.5), radius: 10)
        imgV.image = image
        return imgV
    }()
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

}
